#include "Date.h"

// void Date::Print(const Date* const this)
void Date::Print() const
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

// 任何一个类，只需要写一个> == 或者 < ==重载 剩下比较运算符重载复用即可
bool Date::operator== (const Date& d) const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date::operator>(const Date& d) const
{
	if ((_year > d._year)
		|| (_year == d._year && _month > d._month)
		|| (_year == d._year && _month == d._month && _day > d._day))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

bool Date::operator>=(const Date& d) const
{
	return (*this > d) || (*this == d);
}

bool Date::operator<(const Date& d) const
{
	return !(*this >= d);
}

bool Date::operator<=(const Date& d) const
{
	return !(*this > d);
}

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day) const
{
	Date ret = *this; 
	ret += day;

	return ret;// 出了作用域ret对象就不在了，所以不能用引用返回
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

Date Date::operator-(int day) const
{
	Date ret = *this;
	ret -= day;// ret.operator-=(day);

	return ret;// 和 + 一样，出了作用域ret对象就不在了，所以不能用引用返回
}

Date& Date::operator++() // 前置
{
	return 	*this += 1;
}
Date Date::operator++(int) // 后置
{
	Date ret = *this;
	*this += 1;
	return ret;
}

Date& Date::operator--() // 前置
{
	return *this -= 1;
}
Date Date::operator--(int) // 后置
{
	Date ret = *this;
	*this -= 1;

	return ret;
}

int Date::operator-(const Date& d) const
{
	int ret = 0;
	int flag = -1;
	Date min = *this;//默认第一个小，返回的时候乘上 -1
	Date max = d;

	if (*this > d)//默认错误，把小和大重置，返回时乘上 1
	{
		flag = 1;
		min = d;
		max = *this;
	}

	while (min != max)
	{
		++min;
		++ret;
	}

	return ret * flag;
}

void Date::PrintWeekDay() const //打印*this是星期几
{
	const char* Week[] = { "星期一","星期二" ,"星期三" , "星期四" ,"星期五" , "星期六" , "星期天" };
	Date flag(1900, 1, 1); //1900年1月1日是星期一，自己减自己为0，对应下标0

	cout << Week[(*this - flag) % 7] << endl;
}